# import  numpy as np
# from numpy import*
# arr = [ [1, 2, 3],
#         [2, 5, 6],
#         [7, 8, 9],
#         [7, 7, 7] ]
#
#
# if __name__ == '__main__':
#    x = np.array(arr)   #将数组转换成矩阵
#    b = x.shape[0]
#    c = x.shape[1]
#    a = array(arr)
#    arr1 = a.flatten()   #返回一个折叠成一维的数组。但是该函数只能适用于numpy对象，即array或者mat，普通的list列表是不行的
#    # print(arr1)
#    '''
#    [1 2 3 2 5 6 7 8 9 7 7 7]
#    '''
#    rr = [str(x) for x in arr1]  #将arr1中的每个元素以字符串的形式写入列表rr
#    array = np.array(rr)
#    print(array)
#
#    '''reshape()改变数组的形状，并且原始数据不发生变化。但是reshape()函数中的参数需要满足乘积等于数组中数据总数.'''
#    arr2 = array.reshape(b,c)  #  b为重新生成数组的行数 ，c为重新生成的列数
#    print(arr2)

import numpy as np
from numpy import*
f = open(r"C:/Users/ASUS~/Desktop/data2.txt")
list = f.read()
list = list.split("\n")
f.close()
for i in range(len(list)):
    list[i] = list[i].split()
list1 = np.array(list)
a = list1.shape[0]
b = list1.shape[1]
arr=list1.flatten()
print(arr)
# arr1 = arr.astype(int)
# arr2 = arr1.reshape(a, b)
# print(arr2)

'''
python读取txt文件中的字符串，并将其中的数字输出
''.join()将序列中的元素以指定的字符连接生成一个新的字符串。
语法： 'delimiter'.join(seq)
delimiter：分隔符。可以为空

a=“abcd”          “|”.join([‘a’,‘b’,‘c’])       “,”.join((‘a’,‘b’,‘c’))
“,”.join(a)       ‘a|b|c’                       ‘a,b,c’
‘a,b,c,d’

'''
file = open(r"C:/Users/ASUS~/Desktop/data2.txt")
content = file.read()
s = [i for i in content if str.isdigit(i)]
#['2', '3', '4', '1', '1', '1', '6', '7', '5', '2', '2', '2', '4', '2', '3', '3', '3', '3', '5', '2', '8', '1', '4', '4', '4', '1', '1', '9', '5', '5', '5', '1', '0', '0', '6', '6', '6']
s2 = ' '.join(s)
#2 3 4 1 1 1 6 7 5 2 2 2 4 2 3 3 3 3 5 2 8 1 4 4 4 1 1 9 5 5 5 1 0 0 6 6 6



'''从键盘输入一些字符，逐个把它们写到磁盘文件上，直到输入一个 # 为止'''
if __name__ == '__main__':
    from sys import stdout
    filename = input('输入文件名:\n')
    fp = open(filename,"w")
    ch = input('输入字符串:\n')
    while ch != '#':
        fp.write(ch)
        stdout.write(ch) #等价于 print
        ch = input('')
    fp.close()
